package cn.t.facade.order;


import cn.t.converter.order.ReplenishmentDetailDTOConverter;
import cn.t.core.facade.FacadeImpl;
import cn.t.dto.order.ReplenishmentDetailDTO;
import cn.t.dto.order.repenlement.ReplenishmentDetailSkuDTO;
import cn.t.enums.order.ReplenishmentStatusEnum;
import cn.t.model.order.ReplenishmentDetailDO;
import cn.t.model.sku.SkuDO;
import cn.t.service.order.ReplenishmentDetailService;
import cn.t.service.order.ReplenishmentService;
import cn.t.service.sku.SkuService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 补货订单明细表(ReplenishmentDetail)服务编排
 *
 * @author t
 * @since 2024-05-24 10:19:35
 */
@Slf4j
@Service
@AllArgsConstructor
public class ReplenishmentDetailFacade extends FacadeImpl<ReplenishmentDetailDTO, ReplenishmentDetailDO, ReplenishmentDetailDTOConverter, ReplenishmentDetailService> {
    @Resource
    private ReplenishmentService replenishmentService;
    @Resource
    private SkuService skuService;

    public List<ReplenishmentDetailDTO> getReplenishmentDetailList(List<String> rsSns) {
        List<ReplenishmentDetailDO> byModel = service.getReplenishmentDetailList(rsSns);
        List<String> skuCodes = byModel.stream().map(ReplenishmentDetailDO::getSkuCode).filter(Objects::nonNull).distinct().collect(Collectors.toList());
        Map<String, SkuDO> collect = skuService.findModelList(skuCodes).stream().collect(Collectors.toMap(SkuDO::getSkuCode, Function.identity()));
        return byModel.stream().map(replenishmentDetailDO -> converter.poTDtos(replenishmentDetailDO,
                collect.get(replenishmentDetailDO.getSkuCode()).getSkuImg(), ReplenishmentStatusEnum.getName(replenishmentDetailDO.getRsGoodsStatus()))).collect(Collectors.toList());
    }

    public List<ReplenishmentDetailSkuDTO> replenishmentDetailSkus(String  rsSn) {
        List<ReplenishmentDetailSkuDTO> list = replenishmentService.replenishmentDetailSkuDimension(Collections.singletonList(rsSn));
        list.forEach(d -> {
            d.setRsGoodsStatus(ReplenishmentStatusEnum.getStatusEnum(d.getActualRsQty(), d.getRsQty()).getCode());
        });
        return list;
    }
}

